
import * as Type from '../actionTypes';
import _ from "lodash"
import { NativeModules } from "react-native";
import { TO_ADD, TO_ADD_VALUE } from "../actionTypes";

export default function playBar(state, action) {
  let newState;
  switch (action.type) {
    case Type.PLAY:
      let play = state.play;
      //console.log("目前播放状态是："+play);
      //console.log("打算到达的播放状态是："+action.value);
      newState =  {
        ...state,
        play: action.value,
      };
      return newState;
    case Type.CURRENT_TIME:

      //console.log("目前时间是："+action.value);

      newState =  {
        ...state,
        currentTime: action.value,
      };
      return newState;
    case Type.CURRENT_MUSIC:
      let index = state.musicList.findIndex((r) => {
       newState =  r.id === action.value.id &&   r.name === action.value.name;
      })
      if(action.value.img?.startsWith('//')){
        action.value.img = 'http:' + action.value.img ;
      }
      if(index >-1 ){
        state.musicList[index] = action.value;
      }
      //console.log("切换到："+JSON.stringify(action.value));
      newState =  {
        ...state,
        play:true,
        currentMusic: action.value,
      };
      asy(newState);
      return newState;
    case Type.PLAYLIST:

      //console.log("切换到歌单："+action.value);
      newState =  {
        ...state,
        musicList: action.value,
      };
      asy(newState);
      return newState;
    case Type.ADD_TO_PLAYLIST:
      let newState;
      if(!action.value || action.value === ""){
        return state;
      }
      if(action.value instanceof Array){
        //console.log("添加歌单到播放列表");
        newState = _.concat(action.value,state.musicList);
      }else {
        state.musicList.push(action.value);
        newState = state.musicList;
      }
      //console.log("添加歌单完毕："+JSON.stringify(newState));
      newState =  {
        ...state,
        musicList: newState,
      };
      asy(newState);
      return newState;
    case Type.DEL_TO_PLAYLIST:
      let delnewState = [];
      for (const m of state.musicList) {
        if(!(m.id === action.value.id && m.name === action.value.name)){
          delnewState.push(m);
        }
      }

      //console.log("删除完毕："+delnewState);
      newState =  {
        ...state,
        musicList: delnewState,
      };
      asy(newState);
      return newState;
    case Type.EMPTY_TO_PLAYLIST:

      //console.log("清空歌单!");
      newState =  {
        ...state,
        musicList: [],
      };
      asy(newState);
      return newState;
    case Type.DURATION:

      //console.log("更改当前歌曲持续时间:"+action.value);
      newState =  {
        ...state,
        duration: action.value,
      };
      return newState;
    case Type.ISPRESS:

      //console.log("更改isPress:"+action.value);
      newState =  {
        ...state,
        isPressed: action.value,
      };
      return newState;
    case Type.PLAYER:

      //console.log("更改player:"+action.value);
      newState =  {
        ...state,
        player: action.value,
      };
      return newState;
    case Type.SEEK_DONE:

      //console.log("更改SEEK_DONE:"+action.value);
      newState =  {
        ...state,
        seeked: action.value,
      };
      return newState;
    case Type.PLAY_CONTROL_BOTTOM:

      //console.log("更改PLAY_CONTROL_BOTTOM:"+action.value);
      newState =  {
        ...state,
        playControlBottom: action.value,
      };
      return newState;
    case Type.INIT_STATE:
      //console.log('初始化state：');
      //console.log(action.value);


      return action.value;
    case Type.TO_ADD:
      //console.log('改变toAdd：');
      //console.log(action.value);
      newState =  {
        ...state,
        toAdd: action.value,
      };
      return newState;
    case Type.TO_ADD_VALUE:
      //console.log('改变toAddValue：');
      //console.log(action.value);
      newState =  {
        ...state,
        toAddValue: action.value,
      };
      return newState;

    default:
      newState =  {
        ...state,
      };
      return newState;
  }
}
function asy(state){
  let storeState = {
    currentMusic:state.currentMusic|| {},
    musicList:state.musicList || [],
  }
  let json_state = JSON.stringify(storeState);
  //console.log('同步state:\n' + json_state);
  NativeModules.ToolModule.asyState(json_state).catch(() => {
    //console.log('同步state出错');
  });
}

async function initState(){
  return await NativeModules.ToolModule.getState()
}
